package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.vo.RolesPageBeanVO;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.mapper.SysRoleMapper;
import com.itheima.advantagesup.service.SysRoleService;
import com.itheima.advantagesup.vo.MenuVO;
import com.itheima.advantagesup.vo.RoleVO;
import com.itheima.advantagesup.vo.RoleVO2;
import com.itheima.advantagesup.vo.RoleVO3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public RolesPageBeanVO list(Integer page , Integer limit, String name) {
        //1.设置分页参数
        PageHelper.startPage(page,limit);

        //2.执行查询
        List<SysRole> roles = sysRoleMapper.selectIdListByName(name);
        Page<SysRole> p1 = (Page<SysRole>) roles;
        List<RoleVO> roleVOS = new Page<>();
        for (SysRole role : roles) {
            RoleVO roleVO = new RoleVO();
            roleVO.setId(role.getId());
            roleVO.setName(role.getName());
            roleVO.setDescription(role.getDescription());
            roleVO.setCreate_date(role.getCreateDate());
            roleVO.setUpdate_date(role.getUpdateDate());
            roleVO.setRemarks(role.getRemarks());
            roleVO.setDel_flag(role.getDelFlag());
            roleVO.setSuperadmin(role.getSuperadmin());
            roleVO.setDefault_data(role.getDefault_data());
            roleVOS.add(roleVO);
        }


        Page<RoleVO> p = (Page<RoleVO>) roleVOS;

        //3.封装为对象
        RolesPageBeanVO rolesPageBeanVO = new RolesPageBeanVO(p1.getTotal(),p.getPageNum(),limit,1,p.getResult());

        return rolesPageBeanVO;
    }

    @Override
    public List<RoleVO> allData() {
        List<RoleVO> list = sysRoleMapper.allDate();
        for (RoleVO r : list) {
            r.setRole_id(r.getId());
            List<String> list1 = sysRoleMapper.getMenuId(r.getId());
            String menuId ="";
            for (String s : list1) {
                menuId += s;
            }
            r.setRole_menus(menuId);
        }
        return list;
    }

    @Override
    public void add(RoleVO2 r2) {
        SysRole r1 = new SysRole();
        r1.setName(r2.getName());
        r1.setDescription(r2.getDescription());
        r1.setCreateDate(LocalDateTime.now());
        r1.setUpdateDate(LocalDateTime.now());
        r1.setDelFlag('0');
        if (r2.getMenuids().size()>=35) {
            r1.setSuperadmin(1);
        }else {
            r1.setSuperadmin(0);
        }
        String id = UUID.randomUUID().toString();
        r1.setId(id);
        sysRoleMapper.addRole(r1);
        List<Integer> list = null;
        for (String s : r2.getMenuids()) {
            Integer i = Integer.parseInt(s);
            sysRoleMapper.addRoleAndMenu(r1.getId(),i);
        }
    }

    @Override
    public void delete(String id) {
        sysRoleMapper.delete(id);
        sysRoleMapper.deleteMenuId(id);
    }

    @Override
    public RoleVO3 getInfo(String id) {
        RoleVO3 r = sysRoleMapper.getInfo(id);
        List<String> list = sysRoleMapper.getMenuId(id);
        List<Integer> integers  = new ArrayList<>();
        for (String s : list) {
            integers.add(Integer.valueOf(s));
        }
        r.setMenuids(integers);
        List<MenuVO> menuVOS = new ArrayList<>();
        for (Integer i : integers) {
            menuVOS.add(sysRoleMapper.getMenuByMenuid(i));
        }
        r.setAuthList(menuVOS);
        return r;
    }

    @Override
    public void update(RoleVO3 r) {
        sysRoleMapper.update(r);
        sysRoleMapper.deleteOld(r.getId());
        for (Integer s : r.getMenuids()) {
            sysRoleMapper.addRoleAndMenu(r.getId(),s);
        }
    }
}
